package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Entity.Inf;

public class RegisterFragment extends MyFragment {
    TextView music_preference, prompt_account, prompt_name, prompt_password, prompt_cpassword;
    EditText account, name, password, cpassword;
    RadioGroup male_female;
    Button register;
    int sex = -1;
    String music_text = "", account_text = "", name_text = "", password_text = "", cpassword_text = "";
    boolean account_st = false, name_st = false, password_st = false, cpassword_st = false;

    public RegisterFragment() {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_register, null);
        account = view.findViewById(R.id.register_account);
        name = view.findViewById(R.id.change_old_password);
        password = view.findViewById(R.id.change_password);
        cpassword = view.findViewById(R.id.change_ConfirmPassword);
        music_preference = view.findViewById(R.id.change_music_preference);
        male_female = view.findViewById(R.id.change_male_female);
        register = view.findViewById(R.id.change_button2);
        prompt_account = view.findViewById(R.id.register_prompt_account);
        prompt_name = view.findViewById(R.id.register_prompt_name);
        prompt_password = view.findViewById(R.id.change_prompt_password);
        prompt_cpassword = view.findViewById(R.id.change_prompt_cpassword);
        male_female.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId != -1) {
                    sex = checkedId == R.id.change_radioButton_male ? 1 : 0;
                } else {
                    sex = -1;
                }
            }
        });

        view.findViewById(R.id.change_backbutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(Inf.userFragment);
            }
        });
        view.findViewById(R.id.change_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                music_text = music_preference.getText().toString();
                int last = music_text.lastIndexOf(',');
                if (last != -1)
                    music_preference.setText(music_text.substring(0, last));
                else music_preference.setText("");
            }
        });
        account.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                account_text = account.getText().toString().trim();
                if (!hasFocus && account_text.length() != 0) {
                    if (isValid(account_text, Inf.regex1)) {
                        prompt_account.setText("符合条件");
                        prompt_account.setTextColor(Color.GREEN);
                        account_st = true;
                    } else {
                        prompt_account.setText("输入有误");
                        account_st = false;
                    }
                } else if (hasFocus) {
                    prompt_account.setTextColor(Color.RED);
                    prompt_account.setText(R.string.prompt_detail1);
                }
            }
        });
        name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                name_text = name.getText().toString().trim();
                if (!hasFocus && name_text.length() != 0) {
                    if (isValid(name_text, Inf.regex2)) {
                        prompt_name.setText("符合条件");
                        prompt_name.setTextColor(Color.GREEN);
                        name_st = true;
                    } else {
                        prompt_name.setText("输入有误");
                        name_st = false;
                    }
                } else if (hasFocus) {
                    prompt_name.setTextColor(Color.RED);
                    prompt_name.setText(R.string.prompt_detail2);
                }
            }
        });
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                password_text = password.getText().toString().trim();
                if (!hasFocus && password_text.length() != 0) {
                    if (isValid(password_text, Inf.regex3)) {
                        prompt_password.setText("符合条件");
                        prompt_password.setTextColor(Color.GREEN);
                        password_st = true;
                    } else {
                        prompt_password.setText("输入有误");
                        password_st = false;
                    }
                } else if (hasFocus) {
                    prompt_password.setTextColor(Color.RED);
                    prompt_password.setText(R.string.prompt_detail3);
                    if (cpassword_st) {
                        prompt_cpassword.setTextColor(Color.RED);
                        prompt_cpassword.setText("两次密码不一致");
                        cpassword_st = false;
                    }
                }
            }
        });
        cpassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                cpassword_text = cpassword.getText().toString().trim();
                if (!hasFocus && cpassword_text.length() != 0) {
                    if (cpassword_text.equals(password_text)) {
                        prompt_cpassword.setTextColor(Color.GREEN);
                        prompt_cpassword.setText("符合条件");
                        cpassword_st = true;
                    } else {
                        prompt_cpassword.setTextColor(Color.RED);
                        prompt_cpassword.setText("两次密码不一致");
                        cpassword_st = false;
                    }
                }
            }
        });

        Spinner spinner = view.findViewById(R.id.change_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(Inf.context, android.R.layout.simple_spinner_item, Inf.per_Array);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setSelection(0, false);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position != 0) {
                    String selectedItem = parent.getItemAtPosition(position).toString();
                    if (music_preference.getText().toString().length() != 0)
                        music_preference.append("," + selectedItem);
                    else music_preference.append(selectedItem);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                music_text = music_preference.getText().toString();
                if (music_text.length() == 0) music_text = "无";
                if (cpassword_st && password_st && account_st && name_st && sex != -1) {
                    new RegisterTread().start();
                } else {
                    Toast.makeText(Inf.context, "请填写正确信息", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;
    }

    class RegisterTread extends Thread {
        @Override
        public void run() {
            if (GetResult.Register(account_text, name_text, sex, music_text, password_text) == 1) {
                Looper.prepare();
                Toast.makeText(Inf.context, "注册成功", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
            else {
                Looper.prepare();
                Toast.makeText(Inf.context, "重复账号，请更换账号", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }
    }

    private static boolean isValid(String text, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }
}
